/**
 * www.lifexx.com Inc.
 * Copyright (c) 2014 All Rights Reserved.
 */
package net.mikoo.seals.agent.web.controller;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import net.mikoo.seals.agent.biz.services.PartyServices;
import net.mikoo.seals.agent.biz.services.RoleServices;
import net.mikoo.seals.agent.biz.services.UserLoginServices;
import net.mikoo.seals.agent.common.BaseController;
import net.mikoo.seals.agent.common.PageModel;
import net.mikoo.seals.agent.common.WebContext;
import net.mikoo.seals.agent.common.enums.PartyTypeEnum;
import net.mikoo.seals.agent.common.exception.ParametersException;
import net.mikoo.seals.agent.common.helper.FormHelper;
import net.mikoo.seals.agent.common.utils.MD5Util;
import net.mikoo.seals.agent.model.Party;
import net.mikoo.seals.agent.model.PartyQuery;
import net.mikoo.seals.agent.model.Role;
import net.mikoo.seals.agent.model.UserLogin;

/**
 *                       
 * @Filename OperatorController.java
 *
 * @Description 
 *
 * @Version 1.0
 *
 * @Author chenzx
 *
 * @Email chenzx1206@163.com
 *       
 * @History
 *<li>Author: chenzx</li>
 *<li>Date: 2015年1月9日</li>
 *<li>Version: 1.0</li>
 *<li>Content: create</li>
 *
 */
/**
 * 后台管理员管理
 * @author katy
 *
 */
@Controller
@RequestMapping("/admin/operator/")
public class OperatorController extends BaseController{
	
	Logger logger = Logger.getLogger(OperatorController.class);
	
	@Autowired
	private UserLoginServices userLoginServices;
	
	@Autowired
	private PartyServices partyServices;
	
	@Autowired
	private RoleServices roleService;
	
	/**
	 *操作员管理首页
	 *@param partyQuery
	 *@param request
	 *@param model
	 *@return
	 */
	@RequestMapping("/list")
	public String list(PartyQuery query, HttpServletRequest request, Model model) {

		query.setPartyType(PartyTypeEnum.OPERATOR.code());
		PageModel<Party> pm = partyServices.loadToPage(query);

		model.addAttribute("pm", pm);
		model.addAttribute("query", query);
		
		return resultView(ADMIN_VIEW, "operator/list");
	}

	/**
	 *添加操作员
	 *@param request
	 *@param model
	 *@return
	 */
	@RequestMapping("/form")
	public String form(HttpServletRequest request, Model model) {
		
		List<Role> roles = roleService.selectAllEnabled();
		model.addAttribute("roles", roles);
		return resultView(ADMIN_VIEW, "operator/form");
	}
	
	/**
	 *创建操作员
	 *@param request
	 *@param model
	 *@return
	 */
	@RequestMapping("/create")
	@ResponseBody
	public Map<String,Object> create(HttpServletRequest request, Model model) {
		
		try {
			
			Long partyId = getCurrentPartyId(request,WebContext.ADMIN_LOGIN_SESSION_KEY);
			
			FormHelper form = new FormHelper();
			Party party = form.getCreatOperator(request);
			
			party.setCreatedBy(partyId);
			party.setPartyType(PartyTypeEnum.OPERATOR.code());
			
			partyServices.create(party);
			return result(SUCCESS, MSG_SUCCESS);
		}catch (ParametersException e) {
			return result(ERROR, e.getMessage());
		}catch (Exception e) {
			return result(ERROR, MSG_SYSTEM_ERROR);
		}
		
	}
	
	/**
	 *编辑操作员
	 *@param partyId
	 *@param request
	 *@param model
	 *@return
	 */
	@RequestMapping("/edit")
	public String agentEdit(Long partyId, HttpServletRequest request, Model model) {
		Party party = partyServices.loadByPrimaryKey(partyId);
		List<Role> roles = roleService.selectAllEnabled();

		model.addAttribute("party", party);
		model.addAttribute("roles", roles);
		return resultView(ADMIN_VIEW, "operator/edit");
	}
	
	
	/**
	 * 修改操作员
	 * @param userLogin
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping("/modify")
	@ResponseBody
	public Map<String,Object> modify(HttpServletRequest request, Model model) {
		
		try {
			FormHelper form = new FormHelper();
			Party party = form.getCreatOperator(request);
			partyServices.modifyByPrimaryKey(party);
			return result(SUCCESS, "修改成功");
			
		}catch (ParametersException e) {
			logger.error("",e);
			return result(ERROR, e.getMessage());
		}catch (Exception e) {
			logger.error("",e);
			return result(ERROR, "系统出错");
		}
	}
	
	@RequestMapping("/changeStatus")
	@ResponseBody
	public Map<String,Object> changeStatus(Long partyId, String status, Model model) {
		
		try {
			partyServices.changeStatus(partyId, status);
			return result(SUCCESS, MSG_SUCCESS);
			
		}catch (ParametersException e) {
			logger.error("",e);
			return result(ERROR, e.getMessage());
		}catch (Exception e) {
			logger.error("",e);
			return result(ERROR, MSG_SYSTEM_ERROR);
		}
	}
	
	
	
	

	
	/**
	 * 删除操作员
	 * @param partyId
	 * @param model
	 * @return
	 */
	@RequestMapping("/delete")
	public String delete(Long partyId, Model model) {
//		// 删除当事人表
//		Result<Long> resultParty = partyServices.deleteByPrimaryKey(partyId);
//		// 删除Person表
//		Result<Long> resultPerson = personServices.deleteByPartyId(partyId);
//		// 删除用户登录表
//		Result<Long> resultUserLogin = userLoginServices.deleteByPartyId(partyId);
//		// 删除当事人角色表
//		Result<Long> resultPartyRole = partyRoleService.deleteByPartyId(partyId);
//		if(resultParty.isSuccess() && resultPerson.isSuccess() && resultUserLogin.isSuccess() && resultPartyRole.isSuccess()) {
//			return redirect("/admin/operator/index.htm");
//		}
//		return this.errorTips("", resultParty.getMessage() + resultPerson.getMessage() + resultUserLogin.getMessage(), model);
		return null;
	}
	
	
	
	@RequestMapping("/verifyPassword")
	@ResponseBody
	public Integer verifyPassword(String currentPassword,HttpServletRequest request) {
		UserLogin userLogin = getLoginUser(request, WebContext.ADMIN_LOGIN_SESSION_KEY);
		
		if(MD5Util.Md5(currentPassword).equals(userLogin.getCurrentPassword())){
			return 1;
		}
		return 0;
	}
	
	/**
	 * 修改密码
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping("/operator/editPassword")
	public String editPassword(HttpServletRequest request, Model model) {
		
		UserLogin userLogin = getLoginUser(request, WebContext.ADMIN_LOGIN_SESSION_KEY);
		model.addAttribute("userLogin", userLogin);
		return resultView(ADMIN_VIEW, "operator/editPassword");
	}
	
	@RequestMapping("/operator/modifyPassword")
	@ResponseBody
	public Map<String,Object> modifyPassword(HttpServletRequest request, Model model) {
		try {
			String userLoginId = getCurrentUserLoginId(request, WebContext.ADMIN_LOGIN_SESSION_KEY);
			
			String currentPassword = request.getParameter("currentPassword");
			String newPassword = request.getParameter("newPassword");
			String confPassword = request.getParameter("confPassword");
			
			if(!newPassword.equals(confPassword)){
				return result(ERROR, "两次密码输入不一致");
			}
			
			userLoginServices.changePassword(userLoginId, newPassword,currentPassword);
			
			removeSession(request,WebContext.ADMIN_LOGIN_SESSION_KEY);
			
			return result(SUCCESS, "修改成功");
		}catch (ParametersException e) {
			logger.error("",e);
			return result(ERROR, e.getMessage());
		}catch (Exception e) {
			logger.error("",e);
			return result(ERROR, "系统出错");
		}
		
	}
	
	
}
